package com.guodi.map.utils;

import com.guodi.common.utils.PageData;
import com.guodi.map.dto.MapLayerDTO;
import com.guodi.map.entity.*;
import com.guodi.map.vo.NodeVO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @描述: 递归查询children节点工具类
* @作者: 代鹏刚
* @日期: 2019/12/13 17:09
**/

public class RecursionUtil {
    /**
     * @描述: 通用生成树工具
     * @作者: 代鹏刚
     * @日期: 2019/12/12 18:46
     **/
    public static PageData findChildren(PageData treeNode, List<PageData> treeList){
        for (PageData it : treeList) {
            if(treeNode.getString("id").equals(it.getString("parent_id"))) {
                //递归查询子项
                findChildren(it,treeList);
                if (treeNode.get("children") == null) {
                    List<PageData> children = new ArrayList<>();
                    children.add(it);
                    treeNode.put("children", children);
                }else {
                    List<PageData> children = (List<PageData>)treeNode.get("children");
                    children.add(it);
                    treeNode.put("children", children);
                }
            }
        }
        return treeNode;
    }
    /**
    * @描述: 转换专题目录树
    * @入参: list对象
    * @出参: list对象
    * @作者: 代鹏刚
    * @日期: 2019/12/17 14:30
    **/
    public static List<MapProjectDir> buildProjectDirTree(List<MapProjectDir> dirList){
        //定义返回结果集
        List<MapProjectDir> menuTree = new ArrayList<>();
        for (MapProjectDir mapProjectDir:dirList) {
            if(mapProjectDir.getParentId().equals("-1")){
                menuTree.add(projectDirChildren(mapProjectDir,dirList));
            }
        }
        return menuTree;
    }
    public static MapProjectDir projectDirChildren(MapProjectDir treeNode, List<MapProjectDir> treeList){
        for (MapProjectDir it : treeList) {
            if(treeNode.getId().equals(it.getParentId())) {
                //递归查询子项
                projectDirChildren(it,treeList);
                // 20210330新增图层目录数据处理
                if(treeNode.getType() == 1 && treeNode.getIsLayerDir() == 1){
                    // 图层目录
                    Map<String, Object> map = new HashMap<>();
                    // 获取图层年份
                    String year = it.getAlias().substring(it.getName().indexOf("_")+1);
                    map.put(year, it);
                    // 存储数据
                    if(treeNode.getLayerDirMap() == null){
                        List<Map<String, Object>> maps = new ArrayList<>();
                        maps.add(map);
                        treeNode.setLayerDirMap(maps);
                    }else {
                        List<Map<String, Object>> maps = treeNode.getLayerDirMap();
                        if(!treeNode.getLayerDirMap().contains(map)){
                            maps.add(map);
                        }
                        treeNode.setLayerDirMap(maps);
                    }
                }else {
                    if (treeNode.getChildren() == null) {
                        List<MapProjectDir> children = new ArrayList<>();
                        children.add(it);
                        treeNode.setChildren(children);
                    }else {
                        List<MapProjectDir> children = treeNode.getChildren();
                        if(!children.contains(it)){
                            children.add(it);
                        }
                        treeNode.setChildren(children);
                    }
                }
            }
        }
        return treeNode;
    }

    /**
    * @描述: 获取指定目录下的数据
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2021/3/29 17:11
    **/
    public static List<MapProjectDir> buildAppointTree(List<MapProjectDir> dirList){
        //定义返回结果集
        List<MapProjectDir> menuTree = new ArrayList<>();
        for (MapProjectDir mapProjectDir:dirList) {
            if(mapProjectDir.getIsCommon() != null && mapProjectDir.getIsCommon() == 1){
                menuTree.add(projectDirChildren(mapProjectDir,dirList));
            }
        }
        return menuTree;
    }

    /**
    * @描述: 转换图层目录树
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2022/7/1 15:18
    **/
    public static List<MapLayerDTO> buildLayerDTOTree(List<MapLayerDTO> dirList,String parentId){
        //定义返回结果集
        List<MapLayerDTO> menuTree = new ArrayList<>();
        for (MapLayerDTO mapLayer:dirList) {
            if(mapLayer.getParentId().equals(parentId)){
                menuTree.add(mapLayerDTODirChildren(mapLayer,dirList));
            }
        }
        return menuTree;
    }


    /**
     * @描述: 转换图层目录树
     * @入参:
     * @出参:
     * @作者: 陈宏成
     * @日期: 2021/01/30 16:50:51
     **/
    public static List<MapLayer> buildLayerTree(List<MapLayer> dirList, String parentId){
        //定义返回结果集
        List<MapLayer> menuTree = new ArrayList<>();
        for (MapLayer mapLayer:dirList) {
            if(mapLayer.getParentId().equals(parentId)){
                menuTree.add(mapLayerDirChildren(mapLayer,dirList));
            }
        }
        return menuTree;
    }

    /**
    * @描述: 转换图层目录树
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2019/12/31 19:08
    **/
    public static List<MapLayer> buildLayerTree(List<MapLayer> dirList){
        return buildLayerTree(dirList,"-1");
    }

    /**
    * @描述: 转换图层目录树
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2022/7/1 15:19
    **/
    public static List<MapLayerDTO> buildLayerDTOTree(List<MapLayerDTO> dirList){
        return buildLayerDTOTree(dirList,"-1");
    }

    /**
    * @描述: 递归遍历生成树
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2022/7/1 15:19
    **/
    public static MapLayerDTO mapLayerDTODirChildren(MapLayerDTO treeNode, List<MapLayerDTO> treeList){
        for (MapLayerDTO it : treeList) {
            if(treeNode.getId().equals(it.getParentId())) {
                //递归查询子项
                mapLayerDTODirChildren(it,treeList);
                if (treeNode.getChildren() == null) {
                    List<MapLayerDTO> children = new ArrayList<>();
                    children.add(it);
                    treeNode.setChildren(children);
                }else {
                    List<MapLayerDTO> children = treeNode.getChildren();
                    children.add(it);
                    treeNode.setChildren(children);
                }
            }
        }
        return treeNode;
    }


    public static MapLayer mapLayerDirChildren(MapLayer treeNode, List<MapLayer> treeList){
        for (MapLayer it : treeList) {
            if(treeNode.getId().equals(it.getParentId())) {
                //递归查询子项
                mapLayerDirChildren(it,treeList);
                if (treeNode.getChildren() == null) {
                    List<MapLayer> children = new ArrayList<>();
                    children.add(it);
                    treeNode.setChildren(children);
                }else {
                    List<MapLayer> children = treeNode.getChildren();
                    children.add(it);
                    treeNode.setChildren(children);
                }
            }
        }
        return treeNode;
    }

    /**
    * @描述: 构造专题附件树
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2020/4/7 9:30
    **/
    public static List<NodeVO> buildNodeTree(List<NodeVO> dirList){
        //定义返回结果集
        List<NodeVO> menuTree = new ArrayList<>();
        for (NodeVO nodeVO :dirList) {
            if(nodeVO.getParentId().equals("-1")){
                menuTree.add(nodeChildren(nodeVO,dirList));
            }
        }
        return menuTree;
    }
    public static NodeVO nodeChildren(NodeVO treeNodeVO, List<NodeVO> treeList){
        for (NodeVO it : treeList) {
            if(treeNodeVO.getId().equals(it.getParentId())) {
                //递归查询子项
                nodeChildren(it,treeList);
                if (treeNodeVO.getChildren() == null) {
                    List<NodeVO> children = new ArrayList<>();
                    children.add(it);
                    treeNodeVO.setChildren(children);
                }else {
                    List<NodeVO> children = treeNodeVO.getChildren();
                    children.add(it);
                    treeNodeVO.setChildren(children);
                }
            }
        }
        return treeNodeVO;
    }

    /**
    * @描述: 构造行政区划目录树
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2020/4/7 17:56
    **/
    public static List<MapDivision> recursiveData(List<MapDivision> rootList, List<MapDivision> allList){
        if(rootList != null && rootList.size() > 0){
            for(MapDivision mapDivision : rootList){
                List<MapDivision> childList = allList.stream().filter(p-> p.getParentCode().equals(mapDivision.getCode())).collect(Collectors.toList());
                mapDivision.setChildren(childList);
                recursiveData(childList, allList);
            }
        }
        return rootList;
    }

    /**
     * @描述: 构造功能服务目录树
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2019/12/31 19:08
     **/
    public static List<MapFunctionDir> buildFunctionTree(List<MapFunctionDir> dirList){
        //定义返回结果集
        List<MapFunctionDir> menuTree = new ArrayList<>();
        for (MapFunctionDir mapFunctionDir:dirList) {
            if(mapFunctionDir.getParentId().equals("-1")){
                menuTree.add(functionDirChildren(mapFunctionDir,dirList));
            }
        }
        return menuTree;
    }
    public static MapFunctionDir functionDirChildren(MapFunctionDir treeNode, List<MapFunctionDir> treeList){
        for (MapFunctionDir it : treeList) {
            if(treeNode.getId().equals(it.getParentId())) {
                //递归查询子项
                functionDirChildren(it,treeList);
                if (treeNode.getChildren() == null) {
                    List<MapFunctionDir> children = new ArrayList<>();
                    children.add(it);
                    treeNode.setChildren(children);
                }else {
                    List<MapFunctionDir> children = treeNode.getChildren();
                    children.add(it);
                    treeNode.setChildren(children);
                }
            }
        }
        return treeNode;
    }

    /**
    * @描述: 构造行政区划目录树-非以往方式
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2020/6/11 16:59
    **/
    public static List<MapDivision> buildMapDivision(List<MapDivision> dirList, String parentId){
        //定义返回结果集
        List<MapDivision> menuTree = new ArrayList<>();
        for (MapDivision mapDivision:dirList) {
            if(mapDivision.getParentCode().equals(parentId)){
                menuTree.add(mapDivisionChildren(mapDivision,dirList));
            }
        }
        return menuTree;
    }
    public static MapDivision mapDivisionChildren(MapDivision treeNode, List<MapDivision> treeList){
        for (MapDivision it : treeList) {
            if(treeNode.getCode().equals(it.getParentCode())) {
                //递归查询子项
                mapDivisionChildren(it,treeList);
                if (treeNode.getChildren() == null) {
                    List<MapDivision> children = new ArrayList<>();
                    children.add(it);
                    treeNode.setChildren(children);
                }else {
                    List<MapDivision> children = treeNode.getChildren();
                    children.add(it);
                    treeNode.setChildren(children);
                }
            }
        }
        return treeNode;
    }

    /**
    * @描述: 构造专题附件目录树
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2021/1/20 20:02
    **/
    public static List<MapProjectFile> buildMapProjectFileTree(List<MapProjectFile> dirList, String parentId){
        //定义返回结果集
        List<MapProjectFile> menuTree = new ArrayList<>();
        for (MapProjectFile mapProjectFile:dirList) {
            if(mapProjectFile.getParentId().equals(parentId)){
                menuTree.add(MapProjectFileChildren(mapProjectFile,dirList));
            }
        }
        return menuTree;
    }
    public static MapProjectFile MapProjectFileChildren(MapProjectFile treeNode, List<MapProjectFile> treeList){
        for (MapProjectFile it : treeList) {
            if(treeNode.getId().equals(it.getParentId())) {
                //递归查询子项
                MapProjectFileChildren(it,treeList);
                if (treeNode.getChildren() == null) {
                    List<MapProjectFile> children = new ArrayList<>();
                    children.add(it);
                    treeNode.setChildren(children);
                }else {
                    List<MapProjectFile> children = treeNode.getChildren();
                    children.add(it);
                    treeNode.setChildren(children);
                }
            }
        }
        return treeNode;
    }

    /**
     * @描述: 构造文件汇编目录树
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2021/10/13 11:37
     **/
    public static List<MapMaterialFile> buildMapMaterialFileTree(List<MapMaterialFile> dirList, String parentId){
        //定义返回结果集
        List<MapMaterialFile> menuTree = new ArrayList<>();
        for (MapMaterialFile mapMaterialFile:dirList) {
            if(mapMaterialFile.getParentId().equals(parentId)){
                menuTree.add(mapMaterialFileChildren(mapMaterialFile,dirList));
            }
        }
        return menuTree;
    }
    public static MapMaterialFile mapMaterialFileChildren(MapMaterialFile treeNode, List<MapMaterialFile> treeList){
        for (MapMaterialFile it : treeList) {
            if(treeNode.getId().equals(it.getParentId())) {
                //递归查询子项
                mapMaterialFileChildren(it,treeList);
                if (treeNode.getChildren() == null) {
                    List<MapMaterialFile> children = new ArrayList<>();
                    children.add(it);
                    treeNode.setChildren(children);
                }else {
                    List<MapMaterialFile> children = treeNode.getChildren();
                    children.add(it);
                    treeNode.setChildren(children);
                }
            }
        }
        return treeNode;
    }
}
